﻿using Dapper;
using FRMS.Helper;
using FRMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.DAL
{
    public class BuildDAL
    {
        public bool AddBuild(BuildInfo buildInfo)
        {
            string strSql = "INSERT INTO building_info(BuildingName,projectID)VALUES('" + buildInfo.BuildingName + "'," + buildInfo.projectID + ")";
            return SqlHelper<BuildInfo>.SqlExecute(strSql);
        }

        public int AddBuild_1(BuildInfo buildInfo)
        {
            string strSql = "INSERT INTO building_info(BuildingName,projectID)VALUES('" + buildInfo.BuildingName + "'," + buildInfo.projectID + ");select @@IDENTITY;";
            return SqlHelper<BuildInfo>.SqlScalar(strSql);
        }

        public bool UpdateBuild(BuildInfo buildInfo)
        {
            string strSql = "UPDATE building_info SET BuildingName='" + buildInfo.BuildingName + "',projectID=" + buildInfo.projectID + " WHERE ID=" + buildInfo.ID;
            return SqlHelper<BuildInfo>.SqlExecute(strSql);
        }

        public bool DelBuild(BuildInfo buildInfo)
        {
            string strSql = "UPDATE building_info SET IsDelete=1 WHERE ID=" + buildInfo.ID;
            return SqlHelper<BuildInfo>.SqlExecute(strSql);
        }

        public List<BuildInfo> QueryBuildInfo(BuildInfo buildInfo)
        {
            string strSql = "SELECT * FROM building_info";
            string sqlwhere = string.Empty;

            if (buildInfo.projectID > 0)
            {
                sqlwhere += " projectID=" + buildInfo.projectID + " AND";
            }
            if (!string.IsNullOrEmpty(buildInfo.BuildingName))
            {
                sqlwhere += " BuildingName like '%" + buildInfo.BuildingName + "%' AND";
            }
            sqlwhere += " IsDelete=0 AND";

            if (sqlwhere.Length > 0)
            {
                strSql += " where" + sqlwhere.Substring(0, sqlwhere.Length - 4);
            }
            if (buildInfo.Recordhead > -1 && buildInfo.Recordtail > 0)
            {
                strSql += " LIMIT " + buildInfo.Recordhead + "," + buildInfo.Recordtail;
            }

            return SqlHelper<BuildInfo>.SqlList(strSql);
        }

        public int QueryBuildInfoCount(BuildInfo buildInfo)
        {
            string strSql = "SELECT COUNT(1) FROM building_info";
            string sqlwhere = string.Empty;

            if (buildInfo.projectID > 0)
            {
                sqlwhere += " projectID=" + buildInfo.projectID + " AND";
            }
            if (!string.IsNullOrEmpty(buildInfo.BuildingName))
            {
                sqlwhere += " BuildingName like '%" + buildInfo.BuildingName + "%' AND";
            }
            sqlwhere += " IsDelete=0 AND";

            if (sqlwhere.Length > 0)
            {
                strSql += " where" + sqlwhere.Substring(0, sqlwhere.Length - 4);
            }

            return SqlHelper<BuildInfo>.SqlScalar(strSql);
        }

        public bool CheckRoom(BuildInfo buildInfo)
        {
            string strSql = "SELECT COUNT(1) FROM room_info where IsDelete=0 AND BuildID=" + buildInfo.ID;
            int count = SqlHelper<BuildInfo>.SqlScalar(strSql);
            if (count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public bool CheckElevator(BuildInfo buildInfo)
        {
            string strSql = "SELECT COUNT(1) FROM elevator_info where IsDelete=0 AND BuildID=" + buildInfo.ID;
            int count = SqlHelper<BuildInfo>.SqlScalar(strSql);
            if (count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public BuildInfo LoadBuild(int id)
        {
            string strSql = "SELECT * FROM building_info WHERE ID=" + id;
            return SqlHelper<BuildInfo>.SqlSingleData(strSql);
        }

        public int CheckBuildExist(string name, int projectid)
        {
            string strSql = "SELECT COUNT(1) FROM vi_build WHERE IsDelete=0 AND BuildingName='" + name + "' AND projectID=" + projectid;
            return SqlHelper<BuildInfo>.SqlScalar(strSql);
        }

        public int QueryBuildID(string name, int projectid)
        {
            string strSql = "SELECT ID FROM vi_build WHERE IsDelete=0 AND BuildingName='" + name + "' AND projectID=" + projectid;
            return SqlHelper<BuildInfo>.SqlScalar(strSql);
        }
    }
}
